package com.cnyz.service.impl;

import com.cnyz.base.PageResponse;
import com.cnyz.dto.QuestionDto;
import com.cnyz.entity.Question;
import com.cnyz.mapper.QuestionMapper;
import com.cnyz.service.QuestionService;
import com.cnyz.vo.QuestionVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.config.PageableHandlerMethodArgumentResolverCustomizer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImp implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;
    private PageableHandlerMethodArgumentResolverCustomizer pageableHandlerMethodArgumentResolverCustomizer;

    @Override
    public int addQuestion(QuestionDto questionDto) {
        Question question = new Question();
        BeanUtils.copyProperties(questionDto, question);
        System.out.println("传入的问题"+questionDto);
        System.out.println("生成的问题"+question);
        questionMapper.insertQuestion(question);
        return 0; // 返回0表示操作成功，实际应用中应根据业务逻辑返回相应的值
    }

    @Override
    public void addQuestions(List<QuestionDto> questionDtos) {
        List<Question> questions = questionDtos.stream().map(questionDto -> {
            Question question = new Question();
            BeanUtils.copyProperties(questionDto, question);
            return question;
        }).collect(Collectors.toList());
        questionMapper.insertQuestions(questions);
    }


    @Override
    public PageResponse<QuestionVo> getQuestionPage(Integer pageNum,Integer pageSize,List<Long> questionsId,String subject,Integer type,String file) {

        PageHelper.startPage(pageNum,pageSize);
        List<QuestionVo> questionVos = questionMapper.getQuestionsPage( questionsId,subject, type, file);
        Page<QuestionVo> page = (Page<QuestionVo>) questionVos;
        return PageResponse.of(page, QuestionVo.class);

    }

    @Override
    public void updateQuestion(QuestionDto questionDto) {
        Question question = new Question();
        BeanUtils.copyProperties(questionDto, question);
        questionMapper.updateQuestion(question);
    }
}
